home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0042_Speeding up text files.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-08-25  |  1.8 KB  |  42 lines

  1. {
  2. The most important thing when processing text files is to allocate
  3. a large buffer for reading & writing the files.  By default, TP
  4. allocates 2k for reads & writes. Increasing this buffer to as little
  5. (111 min left), (H)elp, More? as 10k significantly speeds up programs.
  6. Using larger text buffers is painless: you simply set a text buffer.
  7. Before closing the files, you really should do a flush() on any output
  8. text file you're buffering.
  9.  
  10. The following code segment is what I use in my programs to establish
  11. the largest possible text buffer (64k-8, if memory available):
  12. The lines below create a maximum size file buffer for a text file from
  13. memory available on the heap.  Once the buffer has been created and assigned
  14. to the file, i/o can proceed with normal READLN commands.
  15. The buffer is automatically created to the maximum possible size permitted
  16. by TP (64k - 8 bytes), or the largest size permitted by available memory.
  17.  
  18. "Tbuffsize" can be any variable of type LongInt.  It is only used during
  19. the creation of the buffer and can be reused for any purpose.
  20. }
  21.  
  22. {Declarations..}
  23.  
  24. Var
  25.   Target    : Text;    { Text file handle }
  26.   TBuff     : Pointer; { Buffer }
  27.   TBuffsize : LongInt; { Size of buffer }
  28.  
  29. {Code}
  30.  tbuffsize:=Maxavail;                 {Find available memory block}
  31.  if tbuffsize > $fff0                 {Limit to max. data object size}
  32.     then tbuffsize := $fff0;
  33.  getmem(tbuff,tbuffsize);             {Grab memory, hook to pointer}
  34.  settextbuf(target,tbuff^,tbuffsize); {Attach new buffer to text file}
  35.  reset(target);                               {Open file with buffer}
  36.  
  37. {
  38. When processing text on floppy disks, I find this frequently reduces the
  39. program to executing only a single read - which speeds up execution by
  40. a factor of 10.
  41. }
  42.